package com.settlement.system.service.impl;

import com.settlement.system.model.entity.OdsOmsRetailSalesOrderDetails;
import com.settlement.system.mapper.OdsOmsRetailSalesOrderDetailsMapper;
import com.settlement.system.service.OdsOmsRetailSalesOrderDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.OdsOmsRetailSalesOrderDetailsQuery;
import com.settlement.system.model.form.OdsOmsRetailSalesOrderDetailsForm;
import com.settlement.system.model.vo.OdsOmsRetailSalesOrderDetailsImportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;

/**
 * <p>
 * oms订单明细表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-02
 */
@Service
public class OdsOmsRetailSalesOrderDetailsServiceImpl extends ServiceImpl<OdsOmsRetailSalesOrderDetailsMapper, OdsOmsRetailSalesOrderDetails> implements OdsOmsRetailSalesOrderDetailsService {

    @Override
    public Page<OdsOmsRetailSalesOrderDetails> getPage(OdsOmsRetailSalesOrderDetailsQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<OdsOmsRetailSalesOrderDetails> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public List<OdsOmsRetailSalesOrderDetails> getList(OdsOmsRetailSalesOrderDetails details) {
        return this.baseMapper.getList(details);
    }

    @Override
     public boolean saveOdsOmsRetailSalesOrderDetails(OdsOmsRetailSalesOrderDetailsForm odsOmsRetailSalesOrderDetailsForm) {
         OdsOmsRetailSalesOrderDetails odsOmsRetailSalesOrderDetails = new OdsOmsRetailSalesOrderDetails();
         BeanUtils.copyProperties(odsOmsRetailSalesOrderDetailsForm, odsOmsRetailSalesOrderDetails);
         return this.saveOrUpdate(odsOmsRetailSalesOrderDetails);
     }

     @Override
     public OdsOmsRetailSalesOrderDetailsForm getOdsOmsRetailSalesOrderDetailsFormData(Long id) {
         OdsOmsRetailSalesOrderDetails odsOmsRetailSalesOrderDetails = this.getById(id);
         OdsOmsRetailSalesOrderDetailsForm odsOmsRetailSalesOrderDetailsForm = new OdsOmsRetailSalesOrderDetailsForm();
         BeanUtils.copyProperties(odsOmsRetailSalesOrderDetails, odsOmsRetailSalesOrderDetailsForm);
         return odsOmsRetailSalesOrderDetailsForm;
     }

     @Override
     public boolean deleteOdsOmsRetailSalesOrderDetails(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<OdsOmsRetailSalesOrderDetailsImportVo> voList)  {
        List<OdsOmsRetailSalesOrderDetails> odsOmsRetailSalesOrderDetailsList = new ArrayList<>();
        for (OdsOmsRetailSalesOrderDetailsImportVo importVo : voList) {
            OdsOmsRetailSalesOrderDetails odsOmsRetailSalesOrderDetails = new OdsOmsRetailSalesOrderDetails();
            BeanUtils.copyProperties(importVo, odsOmsRetailSalesOrderDetails);
            odsOmsRetailSalesOrderDetailsList.add(odsOmsRetailSalesOrderDetails);
        }
        this.saveOrUpdateBatch(odsOmsRetailSalesOrderDetailsList);
    }

    @Override
    public List<OdsOmsRetailSalesOrderDetailsImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<OdsOmsRetailSalesOrderDetails> odsOmsRetailSalesOrderDetailsList = this.getBaseMapper().selectBatchIds(ids);
        List<OdsOmsRetailSalesOrderDetailsImportVo> voList = new ArrayList<>();
        for (OdsOmsRetailSalesOrderDetails odsOmsRetailSalesOrderDetails : odsOmsRetailSalesOrderDetailsList) {
            OdsOmsRetailSalesOrderDetailsImportVo vo = new OdsOmsRetailSalesOrderDetailsImportVo();
            BeanUtils.copyProperties(odsOmsRetailSalesOrderDetails, vo);
            voList.add(vo);
        }
        return voList;
    }
}
